<?php
class editAction extends sfActions
{
	public function execute($request)
	{
		$this->alias = $request->getParameter('page_alias');
		if ($this->alias) {
			$this->page = Doctrine_Core::getTable('DtPage')->find($this->alias);
			if ($this->page)
				$this->page_form = new AdminPageForm($this->page);
		}
		
		if ($request->hasParameter('page')) {
			$form = $request->getParameter('page');
			$this->status = $this->updatePageFromRequest($this->page, $form);
			if ($this->status) {
				$this->page->save();
				$this->forward('page', 'index');
			} else
				return SfView::SUCCESS;
		} else
			return SfView::SUCCESS;
		
	}
	
	
	private function updatePageFromRequest(&$page, $form)
	{
		if ($form['alias']) {
			$c = Doctrine_Core::getTable('DtPage')->findOneByAlias($form['alias']);
			if (!$c || ($c->alias == $page->alias)) {
				$page->alias = $form['alias'];
			} else {
				return false;
			}
		}
		
		if ($form['name'])
			$page->name = $form['name'];
		if ($form['alias'])
			$page->alias = $form['alias'];
		if ($form['content'])
			$page->content = $form['content'];

		$page->updated_at = date(DATE_ISO8601);
		return true;
	}
}
?>